﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using ConvertEncode;
using System.Globalization;
using System.IO;
using System.Net;
using System.Web;
using Common.errLog;

namespace Common.DBUtility
{
    public class StringPlus
    {
        #region GetStrArray(string str, char speater, bool toLower) 返回一个字符串列表
        /// <summary>
        /// 返回一个字符串列表
        /// </summary>
        /// <param name="str"></param>
        /// <param name="speater"></param>
        /// <param name="toLower"></param>
        /// <returns></returns>
        public static List<string> GetStrArray(string str, char speater, bool toLower)
        {
            List<string> list = new List<string>();
            string[] ss = str.Split(speater);
            foreach (string s in ss)
            {
                if (!string.IsNullOrEmpty(s) && s != speater.ToString())
                {
                    string strVal = s;
                    if (toLower)
                    {
                        strVal = s.ToLower();
                    }
                    list.Add(strVal);
                }
            }
            return list;
        }
        #endregion

        #region GetStrArray(string str) 返回以逗号分割的字符串数组
        /// <summary>
        /// 返回以逗号分割的字符串数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string[] GetStrArray(string str)
        {
            return str.Split(new char[',']);
        }
        public static string GetArrayStr(List<string> list, string speater)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == list.Count - 1)
                {
                    sb.Append(list[i]);
                }
                else
                {
                    sb.Append(list[i]);
                    sb.Append(speater);
                }
            }
            return sb.ToString();
        }
        #endregion

        #region DelLastComma(string str) 删除最后结尾的一个逗号

        /// <summary>
        /// 删除最后结尾的一个逗号
        /// </summary>
        public static string DelLastComma(string str)
        {
            return str.Substring(0, str.LastIndexOf(","));
        }

        /// <summary>
        /// 删除最后结尾的指定字符后的字符
        /// </summary>
        public static string DelLastChar(string str, string strchar)
        {
            return str.Substring(0, str.LastIndexOf(strchar));
        }

        #endregion

        #region ToSBC(string input) 半角转全角的函数(SBC case)
        /// <summary>
        /// 半角转全角的函数(SBC case)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToSBC(string input)
        {
            //半角转全角：
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }
        #endregion

        #region ToDBC(string input) 全角转半角的函数(SBC case)
        /// <summary>
        ///  全角转半角的函数(SBC case)
        /// </summary>
        /// <param name="input">输入</param>
        /// <returns></returns>
        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }
        #endregion

        #region RndString(int strLength = 8)获取随机字符串
        /// <summary> 
        /// 获取随机字符串 
        /// </summary> 
        /// <param name="strLength">字符串长度</param> 
        /// <param name="Seed">随机函数种子值</param> 
        /// <returns>指定长度的随机字符串</returns>
        public static string RndString(int strLength = 8)
        {
            string strSep = ",";
            char[] chrSep = strSep.ToCharArray();
            //因1与l不容易分清楚，所以去除 
            string strChar = "2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z";
            string[] aryChar = strChar.Split(chrSep, strChar.Length);
            string strRandom = string.Empty;
            Random Rnd = new Random();
            //生成随机字符串 
            for (int i = 0; i < strLength; i++)
            {
                strRandom += aryChar[Rnd.Next(aryChar.Length)];
            }
            return strRandom;
        }
        #endregion

        #region RndString(int strLength = 8)获取随数字机字
        /// <summary> 
        /// 获取随数字机字 
        /// </summary> 
        /// <param name="strLength">字符串长度</param> 
        /// <param name="Seed">随机函数种子值</param> 
        /// <returns>指定长度的随机字符串</returns>
        public static string RndNumber(int strLength = 8)
        {
            string strSep = ",";
            char[] chrSep = strSep.ToCharArray();
            //因1与l不容易分清楚，所以去除 
            string strChar = "0,1,2,3,4,5,6,7,8,9";
            string[] aryChar = strChar.Split(chrSep, strChar.Length);
            string strRandom = string.Empty;
            Random Rnd = new Random();
            //生成随机字符串 
            for (int i = 0; i < strLength; i++)
            {
                strRandom += aryChar[Rnd.Next(aryChar.Length)];
            }
            return strRandom;
        }
        #endregion

        #region 获取GUID
        public static string GetGUID()
        {
            return Guid.NewGuid().ToString().Replace("-", "").ToLower();
        }
        public static bool IsGUID(string guid)
        {
            return Regex.IsMatch(guid, @"^[0-9a-z]{32}$");
        }
        #endregion 

        #region GetSubStringList(string o_str, char sepeater)返回指定分割符分割的字符串
        /// <summary>
        /// 返回指定分割符分割的字符串
        /// </summary>
        /// <param name="o_str">字符串</param>
        /// <param name="sepeater">分割符</param>
        /// <returns></returns>
        public static List<string> GetSubStringList(string o_str, char sepeater)
        {
            List<string> list = new List<string>();
            string[] ss = o_str.Split(sepeater);
            foreach (string s in ss)
            {
                if (!string.IsNullOrEmpty(s) && s != sepeater.ToString())
                {
                    list.Add(s);
                }
            }
            return list;
        }
        #endregion

        #region Split(string strIn, string sepeater)分割字符串
        /// <summary>
        /// 分割字符串
        /// </summary>
        /// <param name="strIn"></param>
        /// <param name="sepeater"></param>
        /// <returns></returns>
        public static string[] Split(string strIn, string sepeater)
        {
            return Regex.Split(strIn, sepeater, RegexOptions.IgnoreCase);
        }
        #endregion

        #region 去除字符串中分割符
        /// <summary>
        /// 去除字符串中分割符
        /// </summary>
        /// <param name="StrList"></param>
        /// <param name="SplitString"></param>
        /// <returns></returns>
        public static string GetCleanStyle(string StrList, string SplitString)
        {
            string RetrunValue = "";
            //如果为空，返回空值
            if (StrList == null)
            {
                RetrunValue = "";
            }
            else
            {
                //返回去掉分隔符
                string NewString = "";
                NewString = StrList.Replace(SplitString, "");
                RetrunValue = NewString;
            }
            return RetrunValue;
        }
        #endregion

        #region 判断是字符串是否为中文
        /// <summary>
        /// 判断是字符串是否为中文
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        public static bool IsChinese(string ch)
        {
            byte[] byte_len = System.Text.Encoding.Default.GetBytes(ch);
            if (byte_len.Length == 2) { return true; }

            return false;
        }
        #endregion

        #region 为字符串添加新分割符
        /// <summary>
        /// 为字符串添加新分割符
        /// </summary>
        /// <param name="StrList"></param>
        /// <param name="NewStyle"></param>
        /// <param name="SplitString"></param>
        /// <param name="Error"></param>
        /// <returns></returns>
        public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)
        {
            string ReturnValue = "";
            //如果输入空值，返回空，并给出错误提示
            if (StrList == null)
            {
                ReturnValue = "";
                Error = "请输入需要划分格式的字符串";
            }
            else
            {
                //检查传入的字符串长度和样式是否匹配,如果不匹配，则说明使用错误。给出错误信息并返回空值
                int strListLength = StrList.Length;
                int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length;
                if (strListLength != NewStyleLength)
                {
                    ReturnValue = "";
                    Error = "样式格式的长度与输入的字符长度不符，请重新输入";
                }
                else
                {
                    //检查新样式中分隔符的位置
                    string Lengstr = "";
                    for (int i = 0; i < NewStyle.Length; i++)
                    {
                        if (NewStyle.Substring(i, 1) == SplitString)
                        {
                            Lengstr = Lengstr + "," + i;
                        }
                    }
                    if (Lengstr != "")
                    {
                        Lengstr = Lengstr.Substring(1);
                    }
                    //将分隔符放在新样式中的位置
                    string[] str = Lengstr.Split(',');
                    foreach (string bb in str)
                    {
                        StrList = StrList.Insert(int.Parse(bb), SplitString);
                    }
                    //给出最后的结果
                    ReturnValue = StrList;
                    //因为是正常的输出，没有错误
                    Error = "";
                }
            }
            return ReturnValue;
        }
        #endregion

        #region 去除所有HTML标签  =======================================================================
        public static string NoHTML(string Htmlstring)
        {
            if (string.IsNullOrWhiteSpace(Htmlstring))
                return string.Empty;
            //删除脚本 
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML 
            Htmlstring = Regex.Replace(Htmlstring, @"<br>", "$br$", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<br/>", "$br$", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<br />", "$br$", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"$br$", "<br>", RegexOptions.IgnoreCase);
            //Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "<br>", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            // Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            //Htmlstring.Replace("\r\n", "");
            return Htmlstring;
        }

        public static string RemoveHtml(string src)
        {
            Regex htmlReg = new Regex(@"<[^>]+>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex htmlSpaceReg = new Regex("\\&nbsp\\;", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex spaceReg = new Regex("\\s{2,}|\\ \\;", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex styleReg = new Regex(@"<style(.*?)</style>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex scriptReg = new Regex(@"<script(.*?)</script>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            src = styleReg.Replace(src, string.Empty);
            src = scriptReg.Replace(src, string.Empty);
            src = htmlReg.Replace(src, string.Empty);
            src = htmlSpaceReg.Replace(src, " ");
            src = spaceReg.Replace(src, " ");
            return src.Trim();
        }


        #endregion==========================================================================================

        #region 为用户输入做转义处理
        /// <summary>
        /// 将用户输入的单引号替换成两个单引号
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static string ReplaceMarks(string strInput)
        {
            if (string.IsNullOrEmpty(strInput))
                return string.Empty;
            strInput = strInput.Length >= 20 ? strInput.Substring(0, 20) : strInput;
            return strInput.Replace("'", "''").Replace("[", "[[]").Replace("%", "[%]").Replace("_", "[_]");
        }
        #endregion

        #region 为用户输入做转义处理
        /// <summary>
        /// 将用户输入的单引号替换成两个单引号
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static string ReplaceContainsMarks(string strInput)
        {
            if (string.IsNullOrEmpty(strInput) || strInput.Length < 2)
                return string.Empty;
            string rtnStr = string.Empty;
            strInput = strInput.Length >= 20 ? strInput.Substring(0, 20) : strInput;
            //如果查询条件中包含空格，查询条件需用双引号括起来 否则被认为是语法错误
            //去除干扰词
            strInput = strInput.ToLower();

            strInput = strInput.Replace("or", ";").Replace("；", ";").Replace("|", ";").Replace("and", " ").Replace("'", "''").Replace("\"", "Near").Replace("？", "?").Replace("，", ",").Replace(",", " ");
            strInput = Regex.Replace(strInput, "\\s{2,}", " ");
            strInput = strInput.Replace("? ", "?").Replace(" ?", "?");
            strInput = Regex.Replace(strInput, "\\?{2,}", "?");
            strInput = strInput.TrimEnd('?').TrimEnd(';').TrimEnd(' ');
            strInput = strInput.Replace("&", "＆").Replace("[", "【").Replace("]", "】").Replace("!", "！").Replace("+", "＋").Replace("-", "—").Replace("~", "～");
            //使用空格或者分号进行分词
            var _keyArr = strInput.Split(';');
            foreach (var i in _keyArr)
            {
                if (i.Length < 2)
                    continue;
                rtnStr += string.IsNullOrEmpty(rtnStr) ? i.TrimStart(' ').TrimEnd(' ').Replace(" ", " and ") : " or " + i.TrimStart(' ').TrimEnd(' ').Replace(" ", " and ");
            }
            return rtnStr.TrimEnd(' ').Trim('?').Replace("?", " Near ");
        }
        #endregion

        #region  ChsT0Cht(string inStr)简体转繁体
        /// <summary>
        /// 简体转繁体
        /// </summary>
        /// <param name="inStr"></param>
        /// <returns></returns>
        public static string ChsT0Cht(string inStr)
        {
            EncodeRobert Encode = new EncodeRobert();
            return Encode.SCTCConvert(ConvertType.Simplified, ConvertType.Traditional, inStr);
        }
        #endregion

        #region 文件下载编码处理

        /// <summary>
        /// Encodes non-US-ASCII characters in a string.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToHexString(string s)
        {
            char[] chars = s.ToCharArray();
            StringBuilder builder = new StringBuilder();
            for (int index = 0; index < chars.Length; index++)
            {
                bool needToEncode = NeedToEncode(chars[index]);
                if (needToEncode)
                {
                    string encodedString = ToHexString(chars[index]);
                    builder.Append(encodedString);
                }
                else
                {
                    builder.Append(chars[index]);
                }
            }

            return builder.ToString();
        }

        /// <summary>
        /// Determines if the character needs to be encoded.
        /// </summary>
        /// <param name="chr"></param>
        /// <returns></returns>
        private static bool NeedToEncode(char chr)
        {
            string reservedChars = "$-_.+!*'(),@=&";

            if (chr > 127)
                return true;
            if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0)
                return false;

            return true;
        }

        /// <summary>
        /// Encodes a non-US-ASCII character.
        /// </summary>
        /// <param name="chr"></param>
        /// <returns></returns>
        private static string ToHexString(char chr)
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            byte[] encodedBytes = utf8.GetBytes(chr.ToString());
            StringBuilder builder = new StringBuilder();
            for (int index = 0; index < encodedBytes.Length; index++)
            {
                builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
            }

            return builder.ToString();
        }


        #endregion

        #region 根据扩展名获取MIME
        public static string GetMIMEbyExt(string ext)
        {
            switch (ext.Trim(' '))
            {
                case ".pdf": return "application/pdf";
                case ".doc": return "application/msword";
                case ".docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                case ".ppt": return "application/vnd.ms-powerpoint";
                case ".pptx": return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                case ".xls": return "application/vnd.ms-excel";
                case ".xlsx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                default: return "application/octet-stream";
            }
        }
        #endregion

        #region SubString(string str, int startidx, int len) 中英文混合字符串截取指定长度
        /// <summary>
        /// 字符是否为汉字
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool IsChinese(char c)
        {
            return (int)c >= 0x4E00 && (int)c <= 0x9FA5;
        }
        /// <summary>
        /// 获得字节长度
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static int getLengthb(string str)
        {
            return System.Text.Encoding.Default.GetByteCount(str);
        }
        /// <summary>
        /// 中英文混合字符串截取指定长度,startidx从0开始
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <param name="startidx"></param>
        /// <returns></returns>
        public static string SubString(string str, int len, int startidx = 0, string suffix = "")
        {
            int Lengthb = getLengthb(str);
            if (startidx + 1 > Lengthb)
            {
                return "";
            }
            int j = 0;
            int l = 0;
            int strw = 0;//字符的宽度
            bool b = false;
            string rstr = "";
            for (int i = 0; i < str.Length; i++)
            {
                char c = str[i];
                if (j >= startidx)
                {
                    rstr = rstr + c;
                    b = true;
                }
                if (IsChinese(c))
                {
                    strw = 2;
                }
                else
                {
                    strw = 1;
                }
                j = j + strw;
                if (b)
                {
                    l = l + strw;
                    if ((l + 1) >= len) break;
                }
            }
            return rstr + (Lengthb > len ? suffix : "");
        }
        #endregion

        #region IsValidEmail(string strIn)邮箱格式验证
        /// <summary>
        /// 邮箱格式验证
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidEmail(string strIn)
        {
            if (string.IsNullOrEmpty(strIn))
                return false;
            return Regex.IsMatch(strIn, @"^[a-z0-9-_.]+@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z0-9]{1,18}([\.][a-z0-9]{1,9})*$");
        }
        #endregion

        #region IsValidUserName(string strIn)帐户格式验证
        /// <summary>
        /// 帐户格式验证
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidUserName(string strIn)
        {
            return Regex.IsMatch(strIn, @"^[a-zA-Z0-9_]{4,80}$");
        }
        #endregion

        #region IsValidMobile(string strIn)手机号码验证
        /// <summary>
        /// 手机号码验证
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidMobile(string strIn)
        {
            if (string.IsNullOrEmpty(strIn))
                return false;
            return Regex.IsMatch(strIn, @"^\d{11}$");
        }
        #endregion

        #region IsValidTel(string strIn)手机号码验证
        /// <summary>
        /// 手机号码验证
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidTel(string strIn)
        {
            if (string.IsNullOrEmpty(strIn))
                return false;
            return Regex.IsMatch(strIn, @"^(\d{3,4}-)?\d{6,8}$");
        }
        #endregion

        #region IsValidPostCode(string strIn)邮政编码验证
        /// <summary>
        /// 邮政编码验证
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidPostCode(string strIn)
        {
            if (string.IsNullOrEmpty(strIn))
                return false;
            return Regex.IsMatch(strIn, @"^\d{6}$");
        }
        #endregion

        #region IsValidSpeaterStr(string strIn)以逗号分割的一串数字
        /// <summary>
        /// 逗号分割的一串数字
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidSpeaterStr(string strIn)
        {
            if (string.IsNullOrEmpty(strIn))
                return false;
            return Regex.IsMatch(strIn, @"^[1-9][0-9]{0,9}(,[0-9]{0,10})*|0$");
        }
        #endregion

        #region IsValidMultiClass(string strIn)以逗号分割的一串数字
        /// <summary>
        /// 多选分类验证 以 带号及竖线分割的字符串
        /// </summary>
        /// <param name="strIn"></param>
        /// <returns></returns>
        public static bool IsValidMultiClass(string strIn)
        {
            return Regex.IsMatch(strIn, @"^[0-9]+[|,]?[0-9,|]*[0-9]$");
        }
        #endregion

        #region  NewFileName(string ext)生成上传文件的名称
        /// <summary>
        /// 生成上传文件的名称
        /// </summary>
        /// <param name="ext"></param>
        /// <returns></returns>
        public static string NewFileName(string ext)
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + ext;
        }
        /// <summary>
        /// 获取缩略图名称
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string NewSmallFileName(string url)
        {
            return url.Insert(url.LastIndexOf('/') + 1, "small_");
        }
        #endregion

        #region getUrltoHtml(string Url,string errUrl)根据网址生成静态页
        /// <summary>
        /// 获取指定Web地址的网络页面代码解析
        /// </summary>
        /// <returns>return 返回读取出来的HTML源码</returns>
        public static string GetReaderHtml(string Url, string errUrl)
        {
            try
            {
                HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(Url);
                HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
                //CookieContainer cc = new CookieContainer();
                //HttpWReq.CookieContainer = cc;

                if (HttpWResp.ResponseUri.AbsoluteUri.Split('?')[0].ToLower() != errUrl.Split('?')[0].ToLower())
                {
                    StreamReader sr = new StreamReader(HttpWResp.GetResponseStream(), Encoding.Default);
                    string htmlCode = sr.ReadToEnd();

                    HttpWResp.Close();
                    sr.Close();
                    sr.Dispose();

                    return htmlCode;
                }
            }
            catch (Exception ex)
            {
                errlog.errorlog(ex);
            }
            return null;
        }
        #endregion

        #region 获取电话区号
        /// <summary>
        /// 获取电话区号
        /// </summary>
        /// <param name="tel"></param>
        /// <returns></returns>
        public string getTelZone(string tel)
        {
            string[] ater = tel.Split('-');
            return ater.Length > 1 ? ater[0] : tel.Length == 11 ? tel.Substring(0, 3) : tel;
        }
        #endregion

        #region 获取电话号码
        /// <summary>
        /// 获取电话号码
        /// </summary>
        /// <param name="tel"></param>
        /// <returns></returns>
        public string getTelBody(string tel)
        {
            string[] ater = tel.Split('-');
            if (ater.Length > 1)
            {
                string[] ater1 = ater[1].Split(':');
                return ater1.Length > 1 ? ater1[0] : ater[1];
            }
            else
            {
                string[] ater1 = tel.Split(':');
                return ater1.Length > 1 ? ater1[0] : tel.Length == 11 ? tel.Substring(3, 8) : tel;
            }
        }
        #endregion

        #region 获取分机号码
        /// <summary>
        /// 获取分机号码
        /// </summary>
        /// <param name="tel"></param>
        /// <returns></returns>
        public string getTelExt(string tel)
        {
            string[] ater = tel.Split(':');
            return ater.Length > 1 ? ater[1] : "";
        }
        #endregion

        #region 根据英文周获取中文周
        public static string getWeekByEn(string week)
        {
            switch (week)
            {
                case "Monday": return "一";
                case "Tuesday": return "二";
                case "Wednesday": return "三";
                case "Thursday": return "四";
                case "Friday": return "五";
                case "Saturday": return "六";
                case "Sunday": return "日";
                default: return "";
            }
        }
        public static string getWeekByInt(int week)
        {
            switch (week)
            {
                case 1: return "一";
                case 2: return "二";
                case 3: return "三";
                case 4: return "四";
                case 5: return "五";
                case 6: return "六";
                case 7: return "日";
                default: return "";
            }
        }
        #endregion

        #region 将TimeSpan转换成字符串
        /// <summary>
        /// 将TimeSpan转换成字符串
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public static string TimeSpanToString(TimeSpan ts)
        {
            int d = ts.Days;
            int y = (int)Math.Floor((double)(d / 365));
            int M = (int)Math.Floor((double)(d / 30));
            int h = ts.Hours;
            int m = ts.Minutes;
            int s = ts.Seconds;
            string rtnStr = string.Empty;
            rtnStr = y > 0 ? y + "年以前" : "";
            rtnStr = string.IsNullOrEmpty(rtnStr) ? M > 0 ? M + "月以前" : "" : rtnStr;
            rtnStr = string.IsNullOrEmpty(rtnStr) ? d > 0 ? d + "天以前" : "" : rtnStr;
            rtnStr = string.IsNullOrEmpty(rtnStr) ? h > 0 ? h + "小时以前" : "" : rtnStr;
            rtnStr = string.IsNullOrEmpty(rtnStr) ? m > 0 ? m + "分钟以前" : "" : rtnStr;
            rtnStr = string.IsNullOrEmpty(rtnStr) ? s > 0 ? s + "秒钟以前" : "" : rtnStr;
            return rtnStr;
        }
        #endregion

        #region 提示框消息
        /// <summary>
        /// 提示框消息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static Object Alert(string msg)
        {
            return "<script>alert('" + msg + "');window.close();</script>";
        }
        #endregion

        #region 标签替换
        /// <summary>
        /// 标签替换
        /// </summary>
        /// <param name="strIn">标签字符串</param>
        ///<param name="tmplate">模板</param>
        ///<param name="tmp">替换符</param>
        /// <param name="sep">原分割符</param>
        /// <param name="resep">新分割符</param>
        /// <returns></returns>
        public static string TagReplace(string strIn, string tmplate, string tmp = "{tip}", char sep = ';', string resep = ",")
        {
            if (string.IsNullOrWhiteSpace(strIn))
                return "";
            string[] aIn = strIn.Split(sep);
            string strOut = string.Empty;
            foreach (var i in aIn)
            {
                if (string.IsNullOrWhiteSpace(i))
                    continue;
                strOut += tmplate.Replace(tmp, i) + "｀";
            }
            strOut = strOut.TrimEnd('｀').Replace("｀", resep);
            return strOut;
        }
        #endregion

        #region 随机获取指定长度的文字
        /// <summary>
        /// 随机获邓指定长度的文字
        /// </summary>
        /// <param name="strlength"></param>
        /// <returns></returns>
        public static string GetRangeChineseCode(int strlength)
        {
            //定义一个字符串数组储存汉字编码的组成元素 
            string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

            Random rnd = new Random();

            //定义一个object数组用来 
            object[] bytes = new object[strlength];

            /**/
            /*每循环一次产生一个含两个元素的十六进制字节数组，并将其放入bject数组中 
         每个汉字有四个区位码组成 
         区位码第1位和区位码第2位作为字节数组第一个元素 
         区位码第3位和区位码第4位作为字节数组第二个元素 
        */
            for (int i = 0; i < strlength; i++)
            {
                //区位码第1位 
                int r1 = rnd.Next(11, 14);
                string str_r1 = rBase[r1].Trim();

                //区位码第2位 
                rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值 
                int r2;
                if (r1 == 13)
                {
                    r2 = rnd.Next(0, 7);
                }
                else
                {
                    r2 = rnd.Next(0, 16);
                }
                string str_r2 = rBase[r2].Trim();

                //区位码第3位 
                rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
                int r3 = rnd.Next(10, 16);
                string str_r3 = rBase[r3].Trim();

                //区位码第4位 
                rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
                int r4;
                if (r3 == 10)
                {
                    r4 = rnd.Next(1, 16);
                }
                else if (r3 == 15)
                {
                    r4 = rnd.Next(0, 15);
                }
                else
                {
                    r4 = rnd.Next(0, 16);
                }
                string str_r4 = rBase[r4].Trim();

                //定义两个字节变量存储产生的随机汉字区位码 
                byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
                byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
                //将两个字节变量存储在字节数组中 
                byte[] str_r = new byte[] { byte1, byte2 };

                //将产生的一个汉字的字节数组放入object数组中 
                bytes.SetValue(str_r, i);

            }
            Encoding gb = Encoding.GetEncoding("gb2312");
            string strOutput = string.Empty;

            //根据汉字编码的字节数组解码出中文汉字 
            for (int i = 0; i < strlength; i++)
            {
                strOutput += gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
            }
            return strOutput;
        }
        #endregion

        #region 隐藏邮箱
        /// <summary>
        /// 邮箱
        /// </summary>
        /// <param name="mail"></param>
        /// <returns></returns>
        public static string HideEmail(string mail)
        {
            string _mail = string.Empty;
            if (string.IsNullOrWhiteSpace(mail))
                return "";
            var mailArr = mail.Split('@');
            if (mailArr.Length >= 2)
            {
                if (mailArr[0].Length > 2)
                {
                    string pre = mailArr[0].Substring(0, 1);
                    string end = mailArr[0].Substring(mailArr[0].Length - 1, 1);
                    foreach (var i in mailArr[0])
                    {
                        _mail += "*";
                    }
                    mail = pre + _mail.Substring(0, _mail.Length - 2) + end;
                }
                else
                {
                    foreach (var i in mailArr[0])
                    {
                        _mail += "*";
                    }
                    mail = _mail;
                }
                mail += "@" + mailArr[1];
            }
            return mail;
        }
        #endregion

        #region 隐藏电话
        /// <summary>
        /// 隐藏电话
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string HideMobile(string phone)
        {
            string _phone = string.Empty;
            if (string.IsNullOrWhiteSpace(phone))
                return "";
            if (phone.Length > 4)
            {
                string pre = phone.Substring(0, 1);
                string end = phone.Substring(phone.Length - 3, 3);
                foreach (var i in phone)
                {
                    _phone += "*";
                }
                _phone = pre + _phone.Substring(0, _phone.Length - 4) + end;
            }
            else
            {
                _phone = phone;
            }
            return _phone;
        }
        #endregion

        #region 密码隐藏
        /// <summary>
        /// 密码隐藏
        /// </summary>
        /// <param name="pw"></param>
        /// <returns></returns>
        public static string GetHidePass(string pw)
        {
            if (!string.IsNullOrWhiteSpace(pw) && pw.Length >= 6)
            {
                return pw.Substring(0, pw.Length - 3) + "***";
            }
            else
                return pw;
        }
        #endregion
    }
}
